﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PromotionApp.Utility;
using System.Data.SqlClient;
using PromotionApp.Model;
using System.Data;

namespace PromotionApp.DAL
{
    public class ReplyDAL : BaseDAL
    {
        #region Private Variable
        
        private static ReplyDAL _ReplyDAL = null;

        #endregion

        #region Propery
        #endregion

        #region Constructor

        private ReplyDAL() : base()
        {            
        }

        #endregion

        #region Public Method

        public DataSet GetReplyById(int replyId)
        {            
            string strSql = "TxReplySeleteById";
            List<SqlParameter> list = new List<SqlParameter>();
            list.Add(new SqlParameter("ReplyId", replyId));            
            DataSet ds = this._SQLHelper.GetDataSet(strSql, list, CommandType.StoredProcedure);

            return ds;
        }

        public void UpdateRepleyStatus(int replyId, string strStatusCode, SqlConnection conn)
        {
            string strSql = "TxReplyUpdateStatus";
            List<SqlParameter> list = new List<SqlParameter>();
            list.Add(new SqlParameter("ReplyId", replyId));
            list.Add(new SqlParameter("StatusCode", strStatusCode));
            this._SQLHelper.ExecuteNonQueryTransaction(strSql, list, CommandType.StoredProcedure, conn);            
        }

        public DataSet GetRepliesByTopicIdAndStatus(int topicId, string strStatusCode)
        {
            string strSql = "TxReplySelectByTopicIdAndStatus";
            List<SqlParameter> list = new List<SqlParameter>();

            list.Add(new SqlParameter("TopicId", topicId));
            list.Add(new SqlParameter("StatusCode", strStatusCode));
            DataSet ds = this._SQLHelper.GetDataSet(strSql, list, CommandType.StoredProcedure);

            return ds;
        }

        public void AddReply(Reply reply, SqlConnection conn)
        {
            string strSql = "TxReplyInsert";
            List<SqlParameter> list = new List<SqlParameter>();

            list.Add(new SqlParameter("TopicId", reply.TopicId));            
            list.Add(new SqlParameter("Message", reply.Message));
            list.Add(new SqlParameter("StatusCode", reply.StatusCode));

            if (reply.ImagePath.Trim().Length > 0)
                list.Add(new SqlParameter("ImagePath", reply.ImagePath));                    
            else
                list.Add(new SqlParameter("ImagePath", System.DBNull.Value));
                        
            if (reply.UserName.Trim().Length > 0)
                list.Add(new SqlParameter("UserName", reply.UserName));
            else
                list.Add(new SqlParameter("UserName", System.DBNull.Value));

            if (reply.Email.Trim().Length > 0)
                list.Add(new SqlParameter("Email", reply.Email));                    
            else
                list.Add(new SqlParameter("Email", System.DBNull.Value));                    
            
            this._SQLHelper.ExecuteNonQueryTransaction(strSql, list, CommandType.StoredProcedure, conn);
        }

        public static ReplyDAL GetInstance()
        {
            if (_ReplyDAL == null)
                _ReplyDAL = new ReplyDAL();

            return _ReplyDAL;
        }

        #endregion
    }
}